package cn.edu.zzuli.wechat.user.service.impl;

import cn.edu.zzuli.common.exception.CustomException;
import cn.edu.zzuli.common.utils.NowDate;
import cn.edu.zzuli.model.entity.Points;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.wechat.user.mapper.PointsMapper;
import cn.edu.zzuli.wechat.user.mapper.UserMapper;
import cn.edu.zzuli.wechat.user.service.PointsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;


@Service
@Slf4j
public class PointsServiceImpl extends ServiceImpl<PointsMapper, Points> implements PointsService {

    @Autowired
    private UserMapper userMapper;

    //签到点击
    @Override
    public Map<String, Object> sign(Integer uid, Integer week) {
        Points point = baseMapper.selectOne(new QueryWrapper<Points>().eq("user_id", uid));
        Integer sco = 5;
        Boolean seven = false;
        //如果没签到过
        if(point == null){
            baseMapper.insert(new Points(uid, NowDate.getNowDate(), week));
            point = new Points(uid,1,week,0);
        }else{
            //签过到 进行判断
            long signInTime = NowDate.stringToDates(point.getUpdateTime()).getTime();
            long currTime = NowDate.stringToDates(NowDate.getNowDate()).getTime();
            int daysDiff = (int) (currTime - signInTime)/(60*60*24*1000);
            if (daysDiff <= 0) {
                log.info("重复签到");
                throw new CustomException("今日已经签过到了!");
            }
            if (daysDiff > 1) {
                // 1, 超过一天, 把连续签到的天数重置为 1
                point.setContinueDays(1);
            } else {
                // 2, 没有超过一天, 把连续签到的天数+1
                point.setContinueDays(point.getContinueDays() + 1);
            }
            seven = point.getContinueDays() % 7 == 0;
            if(seven == true){
                sco = 30;
                seven= true;
            }
            point.setUpdateTime(NowDate.getNowDate());
            point.setWeek(week);
            point.setScore(sco + point.getScore());
            baseMapper.updateById(point);
        }

        //已经签到
        User user = new User();
        user.setUid(uid);
        user.setSignIn(2);
        //userMapper.updateById(user);


        Map<String, Object> map = new HashMap<>();
        map.put("total",point.getContinueDays());
        map.put("seven",seven);
        map.put("score",point.getScore() + sco);
        return map;
    }


    //签到查看
    @Override
    public Map<String,Object> res(Integer uid) {
        Map<String,Object> map = new HashMap<>();
        Points point = baseMapper.selectOne(new QueryWrapper<Points>().eq("user_id", uid));
        List<Integer> list = new ArrayList<>(Collections.nCopies(8, 0));
        if (point == null) {
            map.put("total",0);
            map.put("seven",false);
            map.put("score",0);
            map.put("list",list);
            return map;
        }

        Boolean seven = false;
        if(point != null){
            Integer continueDays = point.getContinueDays();
            Integer week = point.getWeek();
            int signDay = continueDays % 7 == 0 ? 7 : continueDays % 7;
            if(signDay == 7){
                seven = true;
            }
            for (int i = 1; i <= signDay; i++){
                if(week < 1){
                    break;
                }
                list.set(week--,1);
            }
        }

        map.put("total",point.getContinueDays());
        map.put("seven",seven);
        map.put("score",point.getScore());
        map.put("list",list);
        return map;
    }

    @Override
    public void changeSco( Integer score, Integer uid) {
        System.out.println("uid:" + uid + "  score:" + score);
        userMapper.updateWealth(uid,score * 1.0 / 100);
        baseMapper.update(new Points(0), new QueryWrapper<Points>().eq("user_id",uid));
    }
}
